package exam.axhu.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import exam.axhu.entity.Choice;
import exam.axhu.entity.Course;
import exam.axhu.entity.Exam;
import exam.axhu.entity.ExamItem;
import exam.axhu.entity.ExamPaper;
import exam.axhu.entity.Judge;
import exam.axhu.entity.ShortAnswer;
import exam.axhu.entity.Student;
import exam.axhu.entity.Teacher;
import exam.axhu.entity.TestItem;
import exam.axhu.entity.TestPaper;
import exam.axhu.entity.TestRecord;
import exam.axhu.service.ChoiceService;
import exam.axhu.service.CourseService;
import exam.axhu.service.ExamService;
import exam.axhu.service.JudgeService;
import exam.axhu.service.ShortAnswerService;
import exam.axhu.service.StudentService;
import exam.axhu.service.TeacherService;


@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class ExamAction extends ActionSupport{
//defaultClass: teacherLogin
	@Resource TeacherService teacherService;
	@Resource StudentService studentService;
	@Resource ExamService examService;
	@Resource CourseService courseService;
	@Resource ChoiceService choiceService;
	@Resource JudgeService judgeService;
	@Resource ShortAnswerService shortAnswerService;
	
	private String teacherID = (String)ActionContext.getContext().getSession().get("teacherID");
	private String studentID = (String)ActionContext.getContext().getSession().get("studentID");
	private String needStudentID;
	private String courseID;
	private String examID;
	private Exam examInfo;
	private ExamPaper examPaper;
	private Choice choice;
	private Judge judge;
	private ShortAnswer shortAnswer;
	private TestItem testItem;
	private TestRecord record1,record2,record3,record4,record5,record6,record7,record8,record9,record10;
	private TestRecord record11,record12,record13,record14,record15,record16,record17,record18,record19,record20;
	private TestRecord record21,record22,record23,record24,record25,record26,record27,record28,record29,record30;
	// 最多15道选择题，10道判断题，5道简答题
	
	public String getNeedStudentID() {
		return needStudentID;
	}
	public void setNeedStudentID(String needStudentID) {
		this.needStudentID = needStudentID;
	}
	public String getCourseID() {
		return courseID;
	}
	public void setCourseID(String courseID) {
		this.courseID = courseID;
	}
	public String getExamID() {
		return examID;
	}
	public void setExamID(String examID) {
		this.examID = examID;
	}
	public Exam getExamInfo() {
		return examInfo;
	}
	public void setExamInfo(Exam examInfo) {
		this.examInfo = examInfo;
	}
	public ExamPaper getExamPaper() {
		return examPaper;
	}
	public void setExamPaper(ExamPaper examPaper) {
		this.examPaper = examPaper;
	}
	public Choice getChoice() {
		return choice;
	}
	public void setChoice(Choice choice) {
		this.choice = choice;
	}
	public Judge getJudge() {
		return judge;
	}
	public void setJudge(Judge judge) {
		this.judge = judge;
	}
	public ShortAnswer getShortAnswer() {
		return shortAnswer;
	}
	public void setShortAnswer(ShortAnswer shortAnswer) {
		this.shortAnswer = shortAnswer;
	}
	public TestItem getTestItem() {
		return testItem;
	}
	public void setTestItem(TestItem testItem) {
		this.testItem = testItem;
	}
	public TestRecord getRecord1() {
		return record1;
	}
	public void setRecord1(TestRecord record1) {
		this.record1 = record1;
	}
	public TestRecord getRecord2() {
		return record2;
	}
	public void setRecord2(TestRecord record2) {
		this.record2 = record2;
	}
	public TestRecord getRecord3() {
		return record3;
	}
	public void setRecord3(TestRecord record3) {
		this.record3 = record3;
	}
	public TestRecord getRecord4() {
		return record4;
	}
	public void setRecord4(TestRecord record4) {
		this.record4 = record4;
	}
	public TestRecord getRecord5() {
		return record5;
	}
	public void setRecord5(TestRecord record5) {
		this.record5 = record5;
	}
	public TestRecord getRecord6() {
		return record6;
	}
	public void setRecord6(TestRecord record6) {
		this.record6 = record6;
	}
	public TestRecord getRecord7() {
		return record7;
	}
	public void setRecord7(TestRecord record7) {
		this.record7 = record7;
	}
	public TestRecord getRecord8() {
		return record8;
	}
	public void setRecord8(TestRecord record8) {
		this.record8 = record8;
	}
	public TestRecord getRecord9() {
		return record9;
	}
	public void setRecord9(TestRecord record9) {
		this.record9 = record9;
	}
	public TestRecord getRecord10() {
		return record10;
	}
	public void setRecord10(TestRecord record10) {
		this.record10 = record10;
	}
	public TestRecord getRecord11() {
		return record11;
	}
	public void setRecord11(TestRecord record11) {
		this.record11 = record11;
	}
	public TestRecord getRecord12() {
		return record12;
	}
	public void setRecord12(TestRecord record12) {
		this.record12 = record12;
	}
	public TestRecord getRecord13() {
		return record13;
	}
	public void setRecord13(TestRecord record13) {
		this.record13 = record13;
	}
	public TestRecord getRecord14() {
		return record14;
	}
	public void setRecord14(TestRecord record14) {
		this.record14 = record14;
	}
	public TestRecord getRecord15() {
		return record15;
	}
	public void setRecord15(TestRecord record15) {
		this.record15 = record15;
	}
	public TestRecord getRecord16() {
		return record16;
	}
	public void setRecord16(TestRecord record16) {
		this.record16 = record16;
	}
	public TestRecord getRecord17() {
		return record17;
	}
	public void setRecord17(TestRecord record17) {
		this.record17 = record17;
	}
	public TestRecord getRecord18() {
		return record18;
	}
	public void setRecord18(TestRecord record18) {
		this.record18 = record18;
	}
	public TestRecord getRecord19() {
		return record19;
	}
	public void setRecord19(TestRecord record19) {
		this.record19 = record19;
	}
	public TestRecord getRecord20() {
		return record20;
	}
	public void setRecord20(TestRecord record20) {
		this.record20 = record20;
	}
	public TestRecord getRecord21() {
		return record21;
	}
	public void setRecord21(TestRecord record21) {
		this.record21 = record21;
	}
	public TestRecord getRecord22() {
		return record22;
	}
	public void setRecord22(TestRecord record22) {
		this.record22 = record22;
	}
	public TestRecord getRecord23() {
		return record23;
	}
	public void setRecord23(TestRecord record23) {
		this.record23 = record23;
	}
	public TestRecord getRecord24() {
		return record24;
	}
	public void setRecord24(TestRecord record24) {
		this.record24 = record24;
	}
	public TestRecord getRecord25() {
		return record25;
	}
	public void setRecord25(TestRecord record25) {
		this.record25 = record25;
	}
	public TestRecord getRecord26() {
		return record26;
	}
	public void setRecord26(TestRecord record26) {
		this.record26 = record26;
	}
	public TestRecord getRecord27() {
		return record27;
	}
	public void setRecord27(TestRecord record27) {
		this.record27 = record27;
	}
	public TestRecord getRecord28() {
		return record28;
	}
	public void setRecord28(TestRecord record28) {
		this.record28 = record28;
	}
	public TestRecord getRecord29() {
		return record29;
	}
	public void setRecord29(TestRecord record29) {
		this.record29 = record29;
	}
	public TestRecord getRecord30() {
		return record30;
	}
	public void setRecord30(TestRecord record30) {
		this.record30 = record30;
	}
	
	
	public String AddExamA(){
		ActionContext.getContext().put("newcourseID",courseID);
		return "AddExam";
	}
	public String AddExamB(){
		if(examInfo.getExamID()==null||examInfo.getName()==null)
		{
			addActionMessage("请输入有效的试卷号和试卷名称");
			return "AddExamFailed";		
		}
		else if(examService.findByExamID(examInfo.getExamID())!=null)
		{	
			addActionMessage("系统检测到该试卷号之前已经注册过!请修改试卷号");
			return "AddExamFailed";			
		}
		
		else{ 
			//System.out.println(courseID);
			examInfo.setCourseID(courseID);
			examService.addExam(examInfo);
			addActionMessage("试卷添加成功!");
			ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
			ActionContext.getContext().put("exam0",examService.listExamsByCourseAndStatus(courseID,0)); //试卷编辑中
			ActionContext.getContext().put("exam2",examService.listExamsByCourseAndStatus(courseID,2)); //试卷审阅中
			ActionContext.getContext().put("exam3",examService.listExamsByCourseAndStatus(courseID,3)); //已批阅试卷
			return "AddExamSucceed";
				
		}
		
	}
	
	public String deleteExamA(){
		ActionContext.getContext().put("deleteexamInfo",examService.findByExamID(examID));
		return "DeleteExam";
	}
	public String deleteExamB(){
		
		if(examInfo!=null)
			examService.deleteExam(examInfo.getExamID());
	
		ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
		ActionContext.getContext().put("exam0",examService.listExamsByCourseAndStatus(courseID,0)); //试卷编辑中
		ActionContext.getContext().put("exam2",examService.listExamsByCourseAndStatus(courseID,2)); //试卷审阅中
		ActionContext.getContext().put("exam3",examService.listExamsByCourseAndStatus(courseID,3)); //已批阅试卷
		return "DeleteExamSucceed";
	}

	public String getInExam(){
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "GetInExam";
	}
	

	public String editChoiceItemA(){
		ActionContext.getContext().put("editexamID", examID);
		ActionContext.getContext().put("editchoice", choiceService.findByID(choice.getChoiceID()));
		return "Edit";
	}
	

	public String editChoiceItemB(){
		Choice newchoice = choiceService.findByID(choice.getChoiceID());
		newchoice.setChoiceA(choice.getChoiceA());
		newchoice.setChoiceB(choice.getChoiceB());
		newchoice.setChoiceC(choice.getChoiceC());
		newchoice.setChoiceD(choice.getChoiceD());
		newchoice.setChoiceContent(choice.getChoiceContent());
		newchoice.setChoiceAnswer(choice.getChoiceAnswer());
		choiceService.update(newchoice);
		addActionMessage("修改成功!欢迎!");
		
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "EditSucceed";
	}
	

	
	public String addChoiceItemA(){
		ActionContext.getContext().put("editexamID", examID);
		ActionContext.getContext().put("choicekus", choiceService.list());
		return "AddChoice";
	}
	
	public String addChoiceItemB(){
	    
		//ExamPaper newitem = new ExamPaper();
		examService.addExamPaper(examID,"Choice",Integer.toString(choice.getChoiceID()));
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "AddSuccessed";
	}

	public String deleteChoiceItemA(){
		ActionContext.getContext().put("deleteChoice", choiceService.findByID(choice.getChoiceID()));
		ActionContext.getContext().put("editexamID", examID);
		return "DeleteChoice";
	}
	
	public String deleteChoiceItemB(){
		if(choice!=null)
			examService.deleteExamPaper(examID, "Choice", Integer.toString(choice.getChoiceID()));
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "DeleteSucceed";
	}
	
	
	
	public String editJudgeItemA(){
		ActionContext.getContext().put("editexamID", examID);
		ActionContext.getContext().put("editjudge", judgeService.findByID(judge.getJudgeID()));
		return "Edit";
	}
	

	public String editJudgeItemB(){
		Judge newjudge = judgeService.findByID(judge.getJudgeID());
		newjudge.setJudgeA(judge.getJudgeA());
		newjudge.setJudgeB(judge.getJudgeB());
		newjudge.setJudgeContent(judge.getJudgeContent());
		newjudge.setJudgeAnswer(judge.getJudgeAnswer());
		judgeService.update(newjudge);
		addActionMessage("修改成功!欢迎!");
		
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "EditSucceed";
	}
	

	
	public String addJudgeItemA(){
		ActionContext.getContext().put("editexamID", examID);
		ActionContext.getContext().put("judgekus", judgeService.list());
		return "AddJudge";
	}
	
	public String addJudgeItemB(){
	    
		//ExamPaper newitem = new ExamPaper();
		examService.addExamPaper(examID,"Judge",Integer.toString(judge.getJudgeID()));
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "AddSuccessed";
	}

	public String deleteJudgeItemA(){
		ActionContext.getContext().put("deleteJudge", judgeService.findByID(judge.getJudgeID()));
		ActionContext.getContext().put("editexamID", examID);
		return "DeleteJudge";
	}
	
	public String deleteJudgeItemB(){
		if(judge!=null)
			examService.deleteExamPaper(examID, "Judge", Integer.toString(judge.getJudgeID()));
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "DeleteSucceed";
	}
	
	
	public String editShortAnswerItemA(){
		ActionContext.getContext().put("editexamID", examID);
		ActionContext.getContext().put("editshortAnswer", shortAnswerService.findByID(shortAnswer.getShortAnswerID()));
		return "Edit";
	}
	

	public String editShortAnswerItemB(){
		ShortAnswer newshortAnswer = shortAnswerService.findByID(shortAnswer.getShortAnswerID());
		newshortAnswer.setShortAnswerContent(shortAnswer.getShortAnswerContent());
		newshortAnswer.setShortAnswerAnswer(shortAnswer.getShortAnswerAnswer());
		shortAnswerService.update(newshortAnswer);
		addActionMessage("修改成功!欢迎!");
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "EditSucceed";
	}
	

	
	public String addShortAnswerItemA(){
		ActionContext.getContext().put("editexamID", examID);
		ActionContext.getContext().put("shortAnswerkus", shortAnswerService.list());
		return "AddShortAnswer";
	}
	
	public String addShortAnswerItemB(){
	    
		//ExamPaper newitem = new ExamPaper();
		examService.addExamPaper(examID,"ShortAnswer",Integer.toString(shortAnswer.getShortAnswerID()));
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "AddSuccessed";
	}

	public String deleteShortAnswerItemA(){
		ActionContext.getContext().put("deleteShortAnswer", shortAnswerService.findByID(shortAnswer.getShortAnswerID()));
		ActionContext.getContext().put("editexamID", examID);
		return "DeleteShortAnswer";
	}
	
	public String deleteShortAnswerItemB(){
		if(shortAnswer!=null)
			examService.deleteExamPaper(examID, "ShortAnswer", Integer.toString(shortAnswer.getShortAnswerID()));
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "DeleteSucceed";
	}
	
	public String editMarkA(){
		ActionContext.getContext().put("edititem", examService.findByExamPaperID(examPaper.getExamPaperID()));
		ActionContext.getContext().put("editexamID", examID);
		return "EditMark";
	}
	
	public String editMarkB(){
		if(examPaper!=null)
		{
			ExamPaper newExamPaper = examService.findByExamPaperID(examPaper.getExamPaperID());
			newExamPaper.setMark(examPaper.getMark());
			examService.updateExamPaper(newExamPaper);
		}
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		List<ExamItem> choices = new ArrayList<ExamItem>();
		if(ems!=null)
			for(ExamPaper em:ems)
				choices.add(new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		List<ExamItem> judges = new ArrayList<ExamItem>();
		if(emss!=null)
			for(ExamPaper em:emss)
				judges.add(new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		List<ExamItem> shortAnswers = new ArrayList<ExamItem>();
		if(emsss!=null)
			for(ExamPaper em:emsss)
				shortAnswers.add(new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
		ActionContext.getContext().put("choices",choices);
		ActionContext.getContext().put("judges",judges);
		ActionContext.getContext().put("shortAnswers",shortAnswers);
		return "EditSucceed";
	}
	
	public String startExamA(){
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		int i = 1;
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		if(ems!=null)
			for(ExamPaper em:ems){
				String cho = "choice"+i;
				ActionContext.getContext().put(cho,new ExamItem(em.getExamPaperID(),choiceService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
				i=i+1;
			}
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID);
		i=1;
		if(emss!=null)
			for(ExamPaper em:emss){
				String cho = "judge"+i;
				ActionContext.getContext().put(cho,new ExamItem(em.getExamPaperID(),judgeService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
				i=i+1;
			}
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		i=1;
		if(emsss!=null)
			for(ExamPaper em:emsss){
				String cho = "shortAnswer"+i;
				ActionContext.getContext().put(cho,new ExamItem(em.getExamPaperID(),shortAnswerService.findByID(Integer.parseInt(em.getItemID())),em.getMark()));
				i=i+1;
			}
		return "startExam";
	}
	public String startExamB(){
		if(record1!=null){
			if(record1.getAnswer()==null||!record1.getAnswer().equals(record1.getRealAnswer().toString())) 
				 record1.setMark(0);
			examService.addTestPaper(record1.getExamPaperID(), studentID, record1.getAnswer(),record1.getMark());
		}
			
		if(record2!=null){
			if(record2.getAnswer()==null||!record2.getAnswer().equals(record2.getRealAnswer().toString())) 
				record2.setMark(0);
			examService.addTestPaper(record2.getExamPaperID(), studentID, record2.getAnswer(),record2.getMark());
		}
		if(record3!=null){
			if(record3.getAnswer()==null||!record3.getAnswer().equals(record3.getRealAnswer().toString())) 
				record3.setMark(0);
			examService.addTestPaper(record3.getExamPaperID(), studentID, record3.getAnswer(),record3.getMark());
		}
		if(record4!=null){
			if(record4.getAnswer()==null||!record4.getAnswer().equals(record4.getRealAnswer().toString())) 
				record4.setMark(0);
			examService.addTestPaper(record4.getExamPaperID(), studentID, record4.getAnswer(),record4.getMark());
		}
		if(record5!=null){
			if(record5.getAnswer()==null||!record5.getAnswer().equals(record5.getRealAnswer().toString())) 
				record5.setMark(0);
			examService.addTestPaper(record5.getExamPaperID(), studentID, record5.getAnswer(),record5.getMark());
		}
		if(record6!=null){
			if(record6.getAnswer()==null||!record6.getAnswer().equals(record6.getRealAnswer().toString())) 
				record6.setMark(0);
			examService.addTestPaper(record6.getExamPaperID(), studentID, record6.getAnswer(),record6.getMark());
		}
		if(record7!=null){
			if(record7.getAnswer()==null||!record7.getAnswer().equals(record7.getRealAnswer().toString())) 
				record7.setMark(0);
			examService.addTestPaper(record7.getExamPaperID(), studentID, record7.getAnswer(),record7.getMark());
		}
		if(record8!=null){
			if(!record8.getAnswer().equals(record8.getRealAnswer().toString())) 
				record8.setMark(0);
			examService.addTestPaper(record8.getExamPaperID(), studentID, record8.getAnswer(),record8.getMark());
		}
		if(record9!=null){
			if(record9.getAnswer()==null||!record9.getAnswer().equals(record9.getRealAnswer().toString())) 
				record9.setMark(0);
			examService.addTestPaper(record9.getExamPaperID(), studentID, record9.getAnswer(),record9.getMark());
		}
		if(record10!=null){
			if(record10.getAnswer()==null||!record10.getAnswer().equals(record10.getRealAnswer().toString())) 
				record10.setMark(0);
			examService.addTestPaper(record10.getExamPaperID(), studentID, record10.getAnswer(),record10.getMark());
		}
		if(record11!=null){
			if(record11.getAnswer()==null||!record11.getAnswer().equals(record11.getRealAnswer().toString())) 
				record11.setMark(0);
			examService.addTestPaper(record11.getExamPaperID(), studentID, record11.getAnswer(),record11.getMark());
		}
		if(record12!=null){
			if(record12.getAnswer()==null||!record12.getAnswer().equals(record12.getRealAnswer().toString())) 
				record12.setMark(0);
			examService.addTestPaper(record12.getExamPaperID(), studentID, record12.getAnswer(),record12.getMark());
		}
		if(record13!=null){
			if(record13.getAnswer()==null||!record13.getAnswer().equals(record13.getRealAnswer().toString())) 
				record13.setMark(0);
			examService.addTestPaper(record13.getExamPaperID(), studentID, record13.getAnswer(),record13.getMark());
		}
		if(record14!=null){
			if(record14.getAnswer()==null||!record14.getAnswer().equals(record14.getRealAnswer().toString())) 
				record14.setMark(0);
			examService.addTestPaper(record14.getExamPaperID(), studentID, record14.getAnswer(),record14.getMark());
		}
		if(record15!=null){
			if(record15.getAnswer()==null||!record15.getAnswer().equals(record15.getRealAnswer().toString())) 
				record15.setMark(0);
			examService.addTestPaper(record15.getExamPaperID(), studentID, record15.getAnswer(),record15.getMark());
		}
		if(record16!=null){
			if(record16.getAnswer()==null||!record16.getAnswer().equals(record16.getRealAnswer().toString())) 
				record16.setMark(0);
			examService.addTestPaper(record16.getExamPaperID(), studentID, record16.getAnswer(),record16.getMark());
		}
		if(record17!=null){
			if(record17.getAnswer()==null||!record17.getAnswer().equals(record17.getRealAnswer().toString())) 
				record17.setMark(0);
			examService.addTestPaper(record17.getExamPaperID(), studentID, record17.getAnswer(),record17.getMark());
		}
		if(record18!=null){
			if(record18.getAnswer()==null||!record18.getAnswer().equals(record18.getRealAnswer().toString())) 
				record18.setMark(0);
			examService.addTestPaper(record18.getExamPaperID(), studentID, record18.getAnswer(),record18.getMark());
		}
		if(record19!=null){
			if(record19.getAnswer()==null||!record19.getAnswer().equals(record19.getRealAnswer().toString())) 
				record19.setMark(0);
			examService.addTestPaper(record19.getExamPaperID(), studentID, record19.getAnswer(),record19.getMark());
		}
		if(record20!=null){
			if(record20.getAnswer()==null||!record20.getAnswer().equals(record20.getRealAnswer().toString())) 
				record20.setMark(0);
			examService.addTestPaper(record20.getExamPaperID(), studentID, record20.getAnswer(),record20.getMark());
		}
		if(record21!=null){
			if(record21.getAnswer()==null||!record21.getAnswer().equals(record21.getRealAnswer().toString())) 
				record21.setMark(0);
			examService.addTestPaper(record21.getExamPaperID(), studentID, record21.getAnswer(),record21.getMark());
		}
		if(record22!=null){
			if(record22.getAnswer()==null||!record22.getAnswer().equals(record22.getRealAnswer().toString())) 
				record22.setMark(0);
			examService.addTestPaper(record22.getExamPaperID(), studentID, record22.getAnswer(),record22.getMark());
		}
		if(record23!=null){
			if(record23.getAnswer()==null||!record23.getAnswer().equals(record23.getRealAnswer().toString())) 
				record23.setMark(0);
			examService.addTestPaper(record23.getExamPaperID(), studentID, record23.getAnswer(),record23.getMark());
		}
		if(record24!=null){
			if(record24.getAnswer()==null||!record24.getAnswer().equals(record24.getRealAnswer().toString())) 
				record24.setMark(0);
			examService.addTestPaper(record24.getExamPaperID(), studentID, record24.getAnswer(),record24.getMark());
		}
		if(record25!=null){
			if(record25.getAnswer()==null||!record25.getAnswer().equals(record25.getRealAnswer().toString())) 
				record25.setMark(0);
			examService.addTestPaper(record25.getExamPaperID(), studentID, record25.getAnswer(),record25.getMark());
		}

		if(record26!=null)
			examService.addTestPaper(record26.getExamPaperID(), studentID, record26.getAnswer());
		if(record27!=null)
			examService.addTestPaper(record27.getExamPaperID(), studentID, record27.getAnswer());
		if(record28!=null)
			examService.addTestPaper(record28.getExamPaperID(), studentID, record28.getAnswer());
		if(record29!=null)
			examService.addTestPaper(record29.getExamPaperID(), studentID, record29.getAnswer());
		if(record30!=null)
			examService.addTestPaper(record30.getExamPaperID(), studentID, record30.getAnswer());
		examService.setStatus(examService.findByExamID(examID), 2);
		return "finishExam";
	}

	public String waitExam(){
		ActionContext.getContext().put("message","试卷正在审阅中!   请耐心等候~");
		return "succeed";
	}
	

	public String afterExam(){
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		int i = 1;
		int total = 0;
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		if(ems!=null)
			for(ExamPaper em:ems){
				String cho = "choice"+i;
				ActionContext.getContext().put(cho,new TestItem(examService.searchTest(em.getExamPaperID(),studentID),em,choiceService.findByID(Integer.parseInt(em.getItemID()))));
				i=i+1;
				total = total + examService.searchTest(em.getExamPaperID(),studentID).getMark();
			}
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID); 
		i=1;
		if(emss!=null)
			for(ExamPaper em:emss){
				String cho = "judge"+i;
				ActionContext.getContext().put(cho,new TestItem(examService.searchTest(em.getExamPaperID(),studentID),em,judgeService.findByID(Integer.parseInt(em.getItemID()))));
				i=i+1;
				total = total + examService.searchTest(em.getExamPaperID(),studentID).getMark();
			}
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		i=1;
		if(emsss!=null)
			for(ExamPaper em:emsss){
				String cho = "shortAnswer"+i;
				ActionContext.getContext().put(cho,new TestItem(examService.searchTest(em.getExamPaperID(),studentID),em,shortAnswerService.findByID(Integer.parseInt(em.getItemID()))));
				i=i+1;
				total = total + examService.searchTest(em.getExamPaperID(),studentID).getMark();
			}
		ActionContext.getContext().put("total",total);
		return "already";
	}
	

	
	public String shenyueA(){
		List<Course> courses = courseService.listStudentsByCourseID(courseID);
		List<Student> students = new ArrayList<Student>();
		for(Course course:courses){
			students.add(studentService.find(course.getStudentID()));
		}
		ActionContext.getContext().put("needCourseID",courseID);
		ActionContext.getContext().put("needExamID",examID);
		ActionContext.getContext().put("students",students);
		return "shenyue";
	}
	public String shenyueB(){
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		ActionContext.getContext().put("needCourseID",courseID);
		ActionContext.getContext().put("needExamID",examID);
		int i = 1;
		/*List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		if(ems!=null)
			for(ExamPaper em:ems){
				String cho = "choice"+i;
				ActionContext.getContext().put(cho,new TestItem(examService.searchTest(em.getExamPaperID(),needStudentID),em,choiceService.findByID(Integer.parseInt(em.getItemID()))));
				i=i+1;
			}
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID); 
		i=1;
		if(emss!=null)
			for(ExamPaper em:emss){
				String cho = "judge"+i;
				ActionContext.getContext().put(cho,new TestItem(examService.searchTest(em.getExamPaperID(),needStudentID),em,judgeService.findByID(Integer.parseInt(em.getItemID()))));
				i=i+1;
			}*/
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		i=1;
		if(emsss!=null)
			for(ExamPaper em:emsss){
				String cho = "shortAnswer"+i;
				ActionContext.getContext().put(cho,new TestItem(examService.searchTest(em.getExamPaperID(),needStudentID),em,shortAnswerService.findByID(Integer.parseInt(em.getItemID()))));
				i=i+1;
			}
		return "shenyueStart";
	}
	public String shenyueC(){
		if(record1!=null)
			examService.updateTestPaper(record1.getTestPaperID(), record1.getMark());
		if(record2!=null)
			examService.updateTestPaper(record2.getTestPaperID(), record2.getMark());
		if(record3!=null)
			examService.updateTestPaper(record3.getTestPaperID(), record3.getMark());
		if(record4!=null)
			examService.updateTestPaper(record4.getTestPaperID(), record4.getMark());
		if(record5!=null)
			examService.updateTestPaper(record5.getTestPaperID(), record5.getMark());
		List<Course> courses = courseService.listStudentsByCourseID(courseID);
		List<Student> students = new ArrayList<Student>();
		for(Course course:courses){
			students.add(studentService.find(course.getStudentID()));
		}
		ActionContext.getContext().put("needCourseID",courseID);
		ActionContext.getContext().put("needExamID",examID);
		ActionContext.getContext().put("students",students);
		return "shenyueSucceed";
	}

	public String shenyueD(){
		examService.setStatus(examService.findByExamID(examID), 3);
		ActionContext.getContext().put("courseInfo",courseService.findByCourseID(courseID));
		ActionContext.getContext().put("exam0",examService.listExamsByCourseAndStatus(courseID,0)); //试卷编辑中
		ActionContext.getContext().put("exam2",examService.listExamsByCourseAndStatus(courseID,2)); //试卷审阅中
		ActionContext.getContext().put("exam3",examService.listExamsByCourseAndStatus(courseID,3)); //已批阅试卷
		return "shenyueFinished";
	}
	
	public String fabu(){
		ActionContext.getContext().put("message","试卷发布成功!");
		examService.setStatus(examService.findByExamID(examID), 1);
		return "succeed";
	}
	
	
	public String okExamA(){
		List<Course> courses = courseService.listStudentsByCourseID(courseID);
		List<Student> students = new ArrayList<Student>();
		for(Course course:courses){
			students.add(studentService.find(course.getStudentID()));
		}
		ActionContext.getContext().put("needCourseID",courseID);
		ActionContext.getContext().put("needExamID",examID);
		ActionContext.getContext().put("students",students);
		return "already";
	}
	public String okExamB(){
		ActionContext.getContext().put("examInfo",examService.findByExamID(examID));
		ActionContext.getContext().put("needCourseID",courseID);
		ActionContext.getContext().put("needExamID",examID);
		int i = 1;
		int total = 0;
		List<ExamPaper> ems = examService.listExamPaperByChoiceItem(examID);
		if(ems!=null)
			for(ExamPaper em:ems){
				String cho = "choice"+i;
				ActionContext.getContext().put(cho,new TestItem(examService.searchTest(em.getExamPaperID(),needStudentID),em,choiceService.findByID(Integer.parseInt(em.getItemID()))));
				i=i+1;
				total = total + examService.searchTest(em.getExamPaperID(),needStudentID).getMark();
			}
		List<ExamPaper> emss = examService.listExamPaperByJudgeItem(examID); 
		i=1;
		if(emss!=null)
			for(ExamPaper em:emss){
				String cho = "judge"+i;
				ActionContext.getContext().put(cho,new TestItem(examService.searchTest(em.getExamPaperID(),needStudentID),em,judgeService.findByID(Integer.parseInt(em.getItemID()))));
				i=i+1;
				total = total + examService.searchTest(em.getExamPaperID(),needStudentID).getMark();
			}
		List<ExamPaper> emsss = examService.listExamPaperByShortAnswerItem(examID);
		i=1;
		if(emsss!=null)
			for(ExamPaper em:emsss){
				String cho = "shortAnswer"+i;
				ActionContext.getContext().put(cho,new TestItem(examService.searchTest(em.getExamPaperID(),needStudentID),em,shortAnswerService.findByID(Integer.parseInt(em.getItemID()))));
				i=i+1;
				total = total + examService.searchTest(em.getExamPaperID(),needStudentID).getMark();
			}
		ActionContext.getContext().put("StudentID",needStudentID);
		ActionContext.getContext().put("total",total);
		return "finished";
	}
}
